import api from '../../../utils/api';
const message = require("../../../utils/message");

// pages/retail/extract/bank.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        retail_id: null,
        has_load: true,
        bank_list: [],
        tab_index: 0,
        bank:{
            type:1,
            bank_name:'',
            card_no:'',
            account_name:'',  //账户名称
            bank_addr:''
            //bank_name:''  ,   //开户行号
        }
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(  options.id ){
            this.setData({
                retail_id: options.id
            })
            this.getRetailUserBankList();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    addRetailUserBank:function () {
        
    },
    getRetailUserBankList:function () {
        var me = this;
        var params = {};
        params.retail_id = this.data.retail_id;

        api.retail.getRetailUserBankList( params , function (response) {
            if(response.status==1){
                me.setData({
                    bank_list: response.data,
                    has_load: true
                })
            }else{

                message.show_error( response.message )
            }
        })
    },
    selectTab:function (e) {
        var status = e.currentTarget.dataset.index;
        this.setData({
            tab_index: status
        })
    },
    swiperChange:function (e) {
        var index = e.detail.current;
        this.setData({
            tab_index: index
        })
        
    },
    doAddBank:function (e) {
        var action= e.currentTarget.dataset.action;

        if(action==1){
            this.setData({
                tab_index:1
            })
        }

        if(action==-1){
            this.setData({
                tab_index:0
            })
        }
        
    },
    selectBankType:function (e) {
        var type = e.currentTarget.dataset.type;
        var bank = this.data.bank;
        bank.type = type;

        this.setData({
            bank:bank
        })
        
    },
    bindInputContent:function (e) {
        var bank = this.data.bank;
        var name = e.currentTarget.dataset.name;

        bank[name] = e.detail.value;

        this.setData({
            bank:bank
        })
        
    },
    addRetailUserBank:function(  ) {

        var me = this;
        var bank = this.data.bank;

        bank.retail_id = this.data.retail_id;

        message.show_loading("正在保存中");
        api.retail.addRetailUserBank(  bank , function ( response ) {
            message.hide_loading();
            if( response.status==1 ){

                message.show_success( response.message , function name(params) {

                    me.setData({
                        tab_index:0
                    })   
                    me.getRetailUserBankList();
                })

            }else{
                message.show_error( response.message )
            }
        })
        
    }
})